//*****************************************************************************
//
//								SlideMenu.c
//
//*****************************************************************************
//
//
#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "printf.h"
#include "util.h"
#include "CPU.h"
#include "I2C.h"
#include "OSD\md_OSD.h"
#include "OSD\dv_OSD.h"
#include "OSD\app_OSD.h"
#include "SPI.h"
#include "Global.h"
#include "SlideMenu.h"
#include "app_MSG.h"


//#include "SPI_Map.h"



#define	SlideTotal 4

#define	TOUCHPRESS			1
#define	TOUCHMOVED			2
#define	TOUCHEND			3
#define	TOUCHCLICK			4
#define	TOUCHDOUBLECLICK	5
#define	TOUCHMOVE			10
#define	TOUCHMOVEUP			11
#define	TOUCHMOVEDOWN		12
#define TOUCHMOVELEFT		13
#define TOUCHMOVERIGHT		14

#define FLASH_DATA_START_ADDRPHOTO 0x690CFF //0xB00000

static WORD	 CurrentSlide;


CONST BITMAP SystemIcon2[]={
	#if 0
	{800,480,16,0,0,0,0x3,547300,0,(FLASH_DATA_START_ADDRPHOTO+0x717)},/* PB070001_16bits.bmp */
    {800,480,16,0,0,0,0x3,584919,1,(FLASH_DATA_START_ADDRPHOTO+0x860fb)},/* PB070002_16bits.bmp */
    {800,480,16,0,0,0,0x3,430857,2,(FLASH_DATA_START_ADDRPHOTO+0x114dd3)},/* PB070003_16bits.bmp */
    {800,480,16,0,0,0,0x3,591271,3,(FLASH_DATA_START_ADDRPHOTO+0x17e0dd)},/* PB070004_16bits.bmp */
    #endif
	{800,480,16,0,0,0,0x4,303511,0,(FLASH_DATA_START_ADDRPHOTO+0x717)},/* Slide1_16bits.bmp */
    {800,480,16,0,0,0,0x4,345094,1,(FLASH_DATA_START_ADDRPHOTO+0x4a8af)},/* Slide2_16bits.bmp */
    {800,480,16,0,0,0,0x5,316615,2,(FLASH_DATA_START_ADDRPHOTO+0x9ecb5)},/* Slide3_16bits.bmp */
    {800,480,16,0,0,0,0x5,306627,3,(FLASH_DATA_START_ADDRPHOTO+0xec17d)},/* Slide4_16bits.bmp */
};

extern CANVAS User_Canvas1[];


//---------- Slide Demo using 16bit OSD -------------------

void InitSlide( BYTE n )
{
	CurrentSlide = n;
	TWLL_OSDLayer_Init();	
	TWLL_Canvas_Init(&User_Canvas1[1]);
	OSD_BlockFill(0,480,800,480,0,16);
	//OSD_ShowWindowBmp(2, 1);				// enable only 16bit
	OSD_LoadBmp((const U8 *)&SystemIcon2[CurrentSlide], 0, 480, 16);
	
	WriteTW88( 0xff, OSD_PAGE );
	WriteTW88( 0xc0, 0x33 );
	WriteTW88( 0xDC, 0 );
	WriteTW88( 0xD6, 0x0f );
	WriteTW88( 0xD7, 0xff );
	WriteTW88( 0xDD, 0x7f );

	OSD_ShowWindowBmp(2, 1);				// enable only 16bit(0, 0);
	OSD_ShowWindowBmp(0, 0);				// disable 8bit
	OSD_ShowWindowBmp(1, 0);				// disable 8bit

}


WORD	SlideAhead;

void SlideUpReady( void )
{

	// 1. image download below the idplay
	CurrentSlide++;
	CurrentSlide %= SlideTotal;
	OSD_LoadBmp((const U8 *)&SystemIcon2[CurrentSlide], 0, 960, 16);
	SlideAhead = 0;
}

void SlideUpStep( WORD step )
{
	SlideAhead += step;
	if ( SlideAhead > 30000 ) SlideAhead = 0;
	if ( SlideAhead >= 470 ) SlideAhead = 470;
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition(2,0, 480+SlideAhead);
}

void SlideUpAfter( void )
{
	WORD	i;
	// 2. SLIDE UP - change vertical display offset
	i = SlideAhead;
	do {
		i += 10;
		OSD_WaitDisplayBlank(1);
		MD_SetWin_MemoryPosition ( 2, 0, 480+i);
	} while ( i < 470 );

	// 3. when finished, block move to display image to origin
	//OsdWriteMode( BLOCKTRANS | BLT_NO | CONV_DIS | PIXEL16 | SOURCE0 );
	OSD_BlockTransfer( 0, 960, 800, 480, 0, 480,16 );
	// 4. change display offset to 0
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition ( 2, 0, 480 );

}

void SlideDownReady( void )
{


	// 1. when finished, block move to display image to origin
	//OsdWriteMode( BLOCKTRANS | BLT_NO | CONV_DIS | PIXEL16 | SOURCE0 );
	OSD_BlockTransfer( 0, 480, 800, 480 ,0, 960,16);
	// 2. change display offset to 0
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition( 2, 0, 960 );
	

	// 3. image download UP the idplay
	//OsdWriteMode( CPUWRITE | BLT_NO | CONV_DIS | PIXEL16 | SOURCE0 );
	//OsdLoadBmpXYFromSPI(  &nHeader, spiS+8, 0, 0 )
	CurrentSlide = CurrentSlide + SlideTotal - 1;
	CurrentSlide %= SlideTotal;
	OSD_LoadBmp((const U8 *)&SystemIcon2[CurrentSlide], 0, 480, 16);

	// 2. SLIDE UP - change vertical display offset
	SlideAhead = 480;
}


void SlideDownStep( WORD step )
{
	SlideAhead -= step;
	if ( SlideAhead > 60000 ) SlideAhead = 0;
	if ( SlideAhead > 480 ) SlideAhead = 480;
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition( 2, 0, 480+SlideAhead);
}

void SlideDownAfter( void )
{
	WORD	i;
	// 2. SLIDE UP - change vertical display offset
	//Printf("\n\rDown=%x\n",SlideAhead);
	i = SlideAhead;
	do {
		i -= 10;
		OSD_WaitDisplayBlank(1);
		MD_SetWin_MemoryPosition ( 2, 0, 480+i);
	} while ( i > 10 );

	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition ( 2, 0, 480);

}


void SlideLeftReady( void )
{
	CurrentSlide++;
	CurrentSlide %= SlideTotal;

	//OsdWriteMode( CPUWRITE | BLT_NO | CONV_DIS | PIXEL16 );
	OSD_LoadBmp((const U8 *)&SystemIcon2[CurrentSlide], 800, 480, 16);

	SlideAhead = 0;

}

void SlideLeftStep( WORD step )
{
	SlideAhead += step;
	if ( SlideAhead > 30000 ) SlideAhead = 0;
	if ( SlideAhead >= 780 ) SlideAhead = 780;
	//Printf("\n\r SlideAhead=%d\n",SlideAhead);
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition( 2, SlideAhead, 480);
	
}

void SlideLeftAfter( void )
{
	WORD	i;
	// 2. SLIDE UP - change vertical display offset
	i = SlideAhead;
	do {
		i += 20;
		OSD_WaitDisplayBlank(1);
		MD_SetWin_MemoryPosition( 2, i, 480);
	} while ( i < 780 );

	// 3. when finished, block move to display image to origin
	//OsdWriteMode( BLOCKTRANS | BLT_NO | CONV_DIS | PIXEL16 | SOURCE0 );
	OSD_BlockTransfer( 800, 480, 800, 480,  0, 480, 16);
	// 4. change display offset to 0
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition( 2, 0, 480 );

}

void SlideRightReady( void )
{
	// 1. when finished, block move to display image to origin
	//OsdWriteMode( BLOCKTRANS | BLT_NO | CONV_DIS | PIXEL16 | SOURCE0 );
	OSD_BlockTransfer( 0, 480, 800, 480, 800, 480, 16);
	// 2. change display offset to 0
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition( 2, 800, 480 );

	// 3. image download UP the idplay
	CurrentSlide = CurrentSlide + SlideTotal - 1;
	CurrentSlide %= SlideTotal;
	OSD_LoadBmp((const U8 *)&SystemIcon2[CurrentSlide], 0, 480, 16);

	// 2. SLIDE RIGHT - change vertical display offset
	SlideAhead = 800;
}

void SlideRightStep( WORD step )
{
	SlideAhead -= step;
	if ( SlideAhead > 60000 ) SlideAhead = 0;
	if ( SlideAhead > 800 ) SlideAhead = 800;
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition( 2, SlideAhead, 480);
}

void SlideRightAfter( void )
{
	WORD	i;
	// 2. SLIDE UP - change vertical display offset
	i = SlideAhead;
	do {
		i -= 20;
		OSD_WaitDisplayBlank(1);
		MD_SetWin_MemoryPosition( 2, i, 480 );
	} while ( i > 20 );
	OSD_WaitDisplayBlank(1);
	MD_SetWin_MemoryPosition( 2, 0, 480 );

}

//---------------------------------------------------------------------------
//             SlideDemo() : Slide Demo like iphone operation
//---------------------------------------------------------------------------
void SlideDemo( void )
{
	BYTE ret;
	BYTE	moveDir;
	BYTE x;
	WORD Direction;
	int i=0;
	CMDST touchcmd;
	static BOOL flag=FALSE;
	WORD	xo, yo;

//	OsdWinDisplay( 0 );						// OFF the display
	moveDir = 0;

	//Printf("\nStarting Touch Slide  mode");

//	OsdInit(4);
	
//	OSD_ShowWindowBmp(2, 0);				// enable only 16bit(0, 0);
	OSD_ShowWindowBmp(0, 0);				// disable 8bit
	OSD_ShowWindowBmp(1, 0);				// disable 8bit
	InitSlide( 0 );
		

	while(1) {
		 	
			ret= CheckTSCCMD(&touchcmd);
			if(ret){
				if(touchcmd.Cmdbuf[0]==0)
				flag=TRUE;
				
				if ( touchcmd.Cmdbuf[0]== 0 ) {
					if (( touchcmd.Cmdbuf[1] > 700 ) && ( (touchcmd.Cmdbuf[2]&0x1fff)  >400 )) {
						break;
					}
					xo=touchcmd.Cmdbuf[3];
					yo=touchcmd.Cmdbuf[4];
			//		Puts("\nTouch Pressed!!!");
				}
				else if(touchcmd.Cmdbuf[0]==2)//(flag&(touchcmd.Cmdbuf[0]==5))
				{
				#if 0
				 Printf("\nx,y, xo,yo,%d   %d    %d  %d\n", touchcmd.Cmdbuf[1], touchcmd.Cmdbuf[2]&0x1fff, touchcmd.Cmdbuf[3], touchcmd.Cmdbuf[4]);
					if(((touchcmd.Cmdbuf[1]-touchcmd.Cmdbuf[3])>50)&(touchcmd.Cmdbuf[1]>touchcmd.Cmdbuf[3]))
						{
							x=TOUCHMOVERIGHT;
							flag=FALSE;
					//		Printf("\n\r Moving Right =%d\n",touchcmd.Cmdbuf[1]);
						}
					else if(((touchcmd.Cmdbuf[3]-touchcmd.Cmdbuf[1])>50)&(touchcmd.Cmdbuf[3]>touchcmd.Cmdbuf[1]))
						{
							x=TOUCHMOVELEFT;
						//	Printf("\n\r Moving Left=%d\n", touchcmd.Cmdbuf[1]);
							flag=FALSE;
						}
					else if(((touchcmd.Cmdbuf[2]&0x1fff-touchcmd.Cmdbuf[4])>50)&(touchcmd.Cmdbuf[2]&0x1fff>touchcmd.Cmdbuf[4]))
						{
							x=TOUCHMOVEDOWN;
							Printf("\n\r Moving Down =%d\n",touchcmd.Cmdbuf[2]&0x1fff);
							flag=FALSE;
						}
					else if(((touchcmd.Cmdbuf[4]-touchcmd.Cmdbuf[2]&0x1fff)>50)&(touchcmd.Cmdbuf[4]>touchcmd.Cmdbuf[2]&0x1fff))
						{
							x=TOUCHMOVEUP;
						//Printf("\n\r Moving up =%d\n", touchcmd.Cmdbuf[2]&0x1fff);
							flag=FALSE;
					}
				#endif
				

					Direction=touchcmd.Cmdbuf[2];
					Direction=Direction>>13;
			//		Printf("\n\r%x  	Direction=%x\n", touchcmd.Cmdbuf[2], Direction);
					if(Direction==0x0)
						x=TOUCHMOVEUP;
					else if(Direction==0x01)
						x=TOUCHMOVEDOWN;
					else if(Direction==0x02)
						x=TOUCHMOVELEFT;
					else if(Direction==0x03)
						x=TOUCHMOVERIGHT;
				
					
				}
				
				if ((touchcmd.Cmdbuf[0]==3))  //POP up  Should Touch Move End
				{
				//	Printf("\n\r Move direction =%bd\n", moveDir);
					//Printf("\n\r Capture POPUP\n");
					if ( moveDir == TOUCHMOVEUP )
						{
							SlideUpAfter( );
							flag=FALSE;
						}
					else if ( moveDir == TOUCHMOVEDOWN )
						{
							SlideDownAfter( );
							flag=FALSE;
						}
					else if ( moveDir == TOUCHMOVELEFT )
						{
							SlideLeftAfter( );
							flag=FALSE;
						}
					else if ( moveDir == TOUCHMOVERIGHT )
						{
							SlideRightAfter( );
							flag=FALSE;
						}
						moveDir = 0;
						x=0;
				}
				else if ( moveDir == 0 ) {
					if ( x == TOUCHMOVEUP ) {
						moveDir = TOUCHMOVEUP;
					//	Printf("\nTouch Moved UP!!!, y0=%d, y1=%d", yo, touchcmd.Cmdbuf[2]&0x1fff);
						SlideUpReady();
						SlideUpStep( (yo-touchcmd.Cmdbuf[2]&0x1fff)/2 );
						yo = touchcmd.Cmdbuf[2]&0x1fff;
					}
					else if ( x == TOUCHMOVEDOWN ) {
						moveDir = TOUCHMOVEDOWN;
					//	Printf("\nTouch Moved DOWN!!!, y0=%d, y1=%d", yo, touchcmd.Cmdbuf[2]&0x1fff);
						SlideDownReady();
						SlideDownStep( (touchcmd.Cmdbuf[2]&0x1fff-yo)/2);
						yo = touchcmd.Cmdbuf[2]&0x1fff;
					}
					else if ( x == TOUCHMOVELEFT ) {
						moveDir = TOUCHMOVELEFT;
					//	Printf("\nTouch Moved LEFT!!!, x0=%d, x1=%d", xo, touchcmd.Cmdbuf[1]);
						SlideLeftReady();
						SlideLeftStep( (xo - touchcmd.Cmdbuf[1])/2 );
						xo = touchcmd.Cmdbuf[1];
					}
					else if ( x == TOUCHMOVERIGHT ) {
						moveDir = TOUCHMOVERIGHT;
					//	Printf("\nTouch Moved RIGHT!!!x0=%d, x1=%d", xo, touchcmd.Cmdbuf[1]);
						SlideRightReady();
						SlideRightStep( (touchcmd.Cmdbuf[1] - xo)/2 );
						xo = touchcmd.Cmdbuf[1];
					}
				}
				else  if ( moveDir == x ) {				// if move at same direction
					if ( x == TOUCHMOVEUP ) {
				//		Printf("\n\r Move Same y up y0=%d, y1=%d \n", yo,touchcmd.Cmdbuf[2]&0x1fff );
						SlideUpStep( yo-(touchcmd.Cmdbuf[2]&0x1fff));
						yo = touchcmd.Cmdbuf[2]&0x1fff;
					}
					else if ( x == TOUCHMOVEDOWN ) {
					//	Printf("\n\r Move Same y down y0=%d, y1=%d \n", yo,touchcmd.Cmdbuf[2]&0x1fff);
						SlideDownStep((touchcmd.Cmdbuf[2]&0x1fff)-yo);
						yo = touchcmd.Cmdbuf[2]&0x1fff;
					}
					else if ( x == TOUCHMOVELEFT ) {
					//	Printf("\n\r Move Same x Left x0=%d, x1=%d \n", xo,touchcmd.Cmdbuf[1]);
						SlideLeftStep( xo - touchcmd.Cmdbuf[1] );
						xo = touchcmd.Cmdbuf[1];
					}
					else if ( x == TOUCHMOVERIGHT ) {
					//	Printf("\n\r Move Same x Right x0=%d, x1=%d \n", xo,touchcmd.Cmdbuf[1]);
						SlideRightStep( touchcmd.Cmdbuf[1] - xo );
						xo = touchcmd.Cmdbuf[1];
					}
				}
			}
		}
	}






